<?php

require_once 'common.php';
// Tell browser to use UTF-8
header('Content-Type: text/html; charset=UTF-8');

// Check if user has logged in to our app
$userFbid = $fb->getUser();
if (!$userFbid) {
	$loginUrl = $fb->getLoginUrl(array('scope' => FB_SCOPE, 'redirect_uri' => FB_URL));
	redirect($loginUrl, 'Facebook authorization', true);
}
if (!empty($_SESSION['me']) && $userFbid != $_SESSION['me']['fbid']) $_SESSION['me'] = array();

// Set up session data
if (empty($_SESSION['me'])) {
	// Add user if it doesn't exist in DB
	if (!$db->q1('SELECT COUNT(*) FROM users WHERE fbid = ? AND date_joined IS NOT NULL', $userFbid)) {
		$accessToken = $fb->getAccessToken();
		$me = $fb->api('/me');
		$db->q('
			INSERT INTO users(fbid, name, date_joined, update_priority, oauth_token)
			VALUES(?, ?, NOW(), 1, ?)
			ON DUPLICATE KEY UPDATE date_joined = NOW(), update_priority = 1, oauth_token = ?
		', $userFbid, $me['name'], $accessToken, $accessToken);
	}
	
	$_SESSION['me'] = $db->qr('SELECT *, UNIX_TIMESTAMP(date_updated) AS unix_date_updated FROM users WHERE fbid = ?', $userFbid);
}

if (!$_SESSION['me']['oauth_token']) {
	$_SESSION['me']['oauth_token'] = $fb->getAccessToken();
	$db->q('UPDATE users SET oauth_token = ? WHERE fbid = ?', $_SESSION['me']['oauth_token'], $_SESSION['me']['fbid']);
}

?>
<!DOCTYPE html>	
<html>
	<head>
		<title>BingoMe</title>
		<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/smoothness/jquery-ui.css">
		<link rel="stylesheet" href="css/jquery.qtip.min.css">
		<link rel="stylesheet" href="css/jquery.multiselect.css">
		<link rel="stylesheet" href="css/style.css">
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
		<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
		<script src="js/jquery.qtip.min.js"></script>
		<script src="js/jquery.multiselect.min.js"></script>
		<script src="https://www.google.com/jsapi"></script>
		<script>google.load('visualization', "1", {packages:["corechart"]});</script>
		<script src="js/ext-all.js"></script>

	</head>
	<body>
		<div id="notice"></div>
		<div id="navigation">
			<a href="//apps.facebook.com/bingome/" target="_top"><img id="logo" src="images/bingome.png"></a>
			<ul id="links">
				<li><a class="<?php if (substr($_SERVER["REQUEST_URI"], -1) == '/' || strpos($_SERVER["REQUEST_URI"], 'index')) echo 'currentPage'; ?>" href="//apps.facebook.com/bingome/" target="_top">Home</a></li>
				<li><a class="<?php if (strpos($_SERVER["REQUEST_URI"], 'posts')) echo 'currentPage'; ?>" href="//apps.facebook.com/bingome/posts.php" target="_top">Wall Posts</a></li>
				<li><a class="<?php if (strpos($_SERVER["REQUEST_URI"], 'likes')) echo 'currentPage'; ?>" href="//apps.facebook.com/bingome/likes.php" target="_top">Likes</a></li>
				<li><a class="<?php if (strpos($_SERVER["REQUEST_URI"], 'checkins')) echo 'currentPage'; ?>" href="//apps.facebook.com/bingome/checkins.php" target="_top">Check-Ins</a></li>
				<li><a class="<?php if (strpos($_SERVER["REQUEST_URI"], 'achievements')) echo 'currentPage'; ?>" href="//apps.facebook.com/bingome/achievements.php" target="_top">Achievements</a></li>
				<li><a class="<?php if (strpos($_SERVER["REQUEST_URI"], 'prizes')) echo 'currentPage'; ?>" href="//apps.facebook.com/bingome/prizes.php" target="_top">Prizes</a></li>
			</ul>
        </div>
		<div id="content">
